<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Request;
use app\index\model\article;
class Zhu extends Controller
{
    
    public function main(){
     return $this->fetch('main');
    }
    public function top(){
     return $this->fetch('top');
    }
    public function indexs(){
     return $this->fetch('indexs');
    }
    public function left(){
     return $this->fetch('left');
    }
    
    public function form(){
        return $this->fetch('form');
    }
    public function errors(){
      return $this->fetch('errors');
    }
    public function filelist(){
      return $this->fetch('filelist');
    }
    public function imglist(){
      $res = DB('upload')->select();
      $this->assign('res',$res);
      return $this->fetch('imglist');
    }
     public function imglist1(){
      return $this->fetch('imglist1');
    }
    public function add(){
        $data = input('post.');
        $data['time'] = time();

        $res = DB('article')->insert($data);
        if ($res) {
            return $this->success('添加成功','zhu/right');
        }else{
            return $this->error('添加失败');
        }
    }
    public function tab(){
          $money=db::table('money')->select();
          // var_dump($money);
          $type=db::table('type')->select();
          // var_dump($type);
          $res=db::table('action')->select();
          // var_dump($res);
        return $this->fetch('tab',['money'=>$money,'type' =>$type, 'res'=>$res]);
    }
    public function geterji()
    {
        $id = input('get.id');
        // print_r($id);die;
        $type=db::table('action')->where('pid='.$id)->select();
        echo json_encode($type);
    }
    public function tabs(){
        $data = input('post.');
        $data['action_id'] = implode('-', $data['action_id']);
        $res = DB('product')->insert($data);
        // print_r($res);die;
        if ($res) {
            return $this->success('添加成功','zhu/main');
        }else{
            return $this->error('添加失败');
        }
    }


     public function right(){
        //$res = DB('article')->select();
        $Db = new article();
        $res = $Db->show();
        $this->assign('res',$res);
        return $this->fetch('right');
     } 
   
     public function ajaxDel(){
    $id = input('get.id');
    $res = DB('article')->where('id',$id)->delete();
    if ($res) {
      return 1;
    }else{
        return 0;
    }
   }


//图片添加
    public function adds(){
      $data = input('post.');
      $data['img'] = $this->upload();
      $res = DB('upload')->insert($data);
      if ($res) {
        return $this->success('新增成功','zhu/imglist');
      }else{
        return $this->error('新增失败');
      }
    }

    //文件上传
    public function upload(){
    // 获取表单上传文件 例如上传了001.jpg
    $file = request()->file('img');
    // 移动到框架应用根目录/public/uploads/ 目录下
    $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
    if($info){
       
        // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
        return $info->getSaveName();
        
    }else{
        // 上传失败获取错误信息
        echo $file->getError();
    }
}



 public function delAll(){
        //接收传值
        $id = Request::instance()->param('id');
        $ids = explode(',',$id);
        $ids = input('get.id');
        $reg = DB("article")->delete($ids);
        // print_r($reg);die;
        if($reg){
            echo 1;//成功输出1
        }else{
            echo 2;//失败输出2
        }
    }

}
?>